package com.rc.saas.mini.controller.invoice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.base.MinipBaseController;
import com.rc.saas.mini.common.base.DataTResponse;
import com.rc.saas.mini.common.base.TResponse;
import com.rc.saas.mini.common.config.UpYunConfig;
import com.rc.saas.mini.common.constant.SessionConstants;
import com.rc.saas.mini.minip.vo.LoginInfoVo;
import com.rc.saas.mini.model.enterprise.EnterpriseMemberApply;
import com.rc.saas.mini.model.invoice.Invoice;
import com.rc.saas.mini.model.invoice.InvoiceAddress;
import com.rc.saas.mini.model.invoice.InvoiceAddressExample;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.member.MemberInvoiceInfo;
import com.rc.saas.mini.service.invoice.InvoiceAddressService;
import com.rc.saas.mini.service.invoice.InvoiceApplyService;
import com.rc.saas.mini.service.invoice.InvoiceService;
import com.rc.saas.mini.service.member.MemberInfoService;
import com.rc.saas.mini.service.member.MemberInvoiceInfoService;
import com.rc.saas.mini.service.order.OrderInfoService;
import com.rc.saas.mini.service.tenant.TenantOpenServiceService;
import com.rc.saas.mini.service.tenant.TenantSettingService;
import com.rc.saas.mini.vo.base.RcPageVo;
import com.rc.saas.mini.vo.invoice.InvoiceApplyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/4/28
 */
@Api(tags = "14.发票相关")
@Controller
@RequestMapping("/invoice")
public class InvoiceController extends MinipBaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${invoice.host}")
    private String invoiceHost;
    @Value("${invoice.path}")
    private String invoicePath;
    @Value("${invoice.method}")
    private String invoiceMethod;
    @Value("${invoice.code}")
    private String invoiceCode;

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private InvoiceApplyService invoiceApplyService;

    @Autowired
    private MemberInfoService memberInfoService;

    @Autowired
    private TenantOpenServiceService tenantOpenServiceService;

    @Autowired
    private InvoiceService invoiceService;

    @Autowired
    private TenantSettingService tenantSettingService;

    @Autowired
    private InvoiceAddressService invoiceAddressService;

    @Autowired
    private MemberInvoiceInfoService memberInvoiceInfoService;

    @Autowired
    private UpYunConfig upYunConfig;


    ///**
    // * 发票记录
    // */
    //@ResponseBody
    //@RequestMapping(value = "/listPageInvoice", method = RequestMethod.POST)
    //@ApiOperation(value = "发票记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    //@ApiImplicitParams({
    //        @ApiImplicitParam(name = "pageNo", paramType = "query", value = "页码", required = true),
    //        @ApiImplicitParam(name = "pageSize", paramType = "query", value = "页量", required = true),
    //        @ApiImplicitParam(name = "invoiceNormalType", paramType = "query", value = "开票类型 0个人开票 1企业开票", required = true),
    //})
    //public DataTResponse<PageInfo<InvoiceApplyVo>> listPageInvoice(HttpServletRequest request, String invoiceNormalType,
    //                                                               @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
    //                                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
    //    String tenantCode = (String) request.getSession().getAttribute(SessionConstants.SESSION_TENANT);
    //    if (tenantCode == null || StringUtils.isBlank(tenantCode)) {
    //        logger.error("找不到对应运营商信息:{}", tenantCode);
    //        return DataTResponse.FAIL("找不到对应运营商信息");
    //    }
    //    LoginInfoVo loginInfo = readLoginInfo(request);
    //    if (loginInfo == null) {
    //        return DataTResponse.FAIL_LOGIN();
    //    }
    //    MemberInfo member = memberInfoService.selectByPrimaryKey(loginInfo.getMemberId());
    //    if (member == null) {
    //        return DataTResponse.FAIL_LOGIN();
    //    }
    //    logger.info("InvoiceController.listPageInvoice.tenantCode:{},mobile:{}", tenantCode, member.getMobile());
    //
    //    //开票申请记录
    //    PageInfo<InvoiceApplyVo> pageInfo = invoiceApplyService.listPageInvoiceApply(loginInfo.getMemberId(), invoiceNormalType, pageNo, pageSize);
    //    return DataTResponse.SUCCESS_DATA(pageInfo);
    //}

    /**
     * 开具个人发票"
     */
    @ResponseBody
    @RequestMapping(value = "/addPersonalInvoice", method = RequestMethod.POST)
    @ApiOperation(value = "开具发票", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "address", paramType = "query", value = "收件人地址", required = false),
            @ApiImplicitParam(name = "amount", paramType = "query", value = "开票金额", required = true),
            @ApiImplicitParam(name = "invoiceTitle", paramType = "query", value = "发票抬头", required = true),
            @ApiImplicitParam(name = "invoiceType", paramType = "query", value = "发票类型   0普通发票 1增值税专用发票 2增值税普通发票-电子发票 3增值税普通发票-纸质发票  默认0", required = true),
            @ApiImplicitParam(name = "areaId", paramType = "query", value = "区Id", required = false),
            @ApiImplicitParam(name = "cityId", paramType = "query", value = "市Id", required = false),
            @ApiImplicitParam(name = "provinceId", paramType = "query", value = "省Id", required = false),
            @ApiImplicitParam(name = "addresseeTel", paramType = "query", value = "联系电话", required = false),
            @ApiImplicitParam(name = "companyCredit", paramType = "query", value = "纳税人识别号", required = false),
            @ApiImplicitParam(name = "companyEmail", paramType = "query", value = "邮箱地址", required = true),
            @ApiImplicitParam(name = "companyAddress", paramType = "query", value = "公司注册地址", required = false),
            @ApiImplicitParam(name = "companyTel", paramType = "query", value = "公司注册时联系号码", required = false),
            @ApiImplicitParam(name = "companyBank", paramType = "query", value = "开户行", required = false),
            @ApiImplicitParam(name = "companyBankAccount", paramType = "query", value = "公司银行账号", required = false),
            @ApiImplicitParam(name = "orderIdStr", paramType = "query", value = "订单编号（多个用,隔开  注: 英文,）", required = true),
            @ApiImplicitParam(name = "addressee", paramType = "query", value = "收件人", required = false)
    })
    public DataTResponse<String> addPersonalInvoice(HttpServletRequest request, InvoiceApplyVo invoiceApplyVo) {
        String tenantCode = (String) request.getSession().getAttribute(SessionConstants.SESSION_TENANT);
        if (tenantCode == null || StringUtils.isBlank(tenantCode)) {
            logger.error("找不到对应运营商信息:{}", tenantCode);
            return DataTResponse.FAIL("找不到对应运营商信息");
        }

        LoginInfoVo loginInfo = readLoginInfo(request);
        if (loginInfo == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        MemberInfo member = memberInfoService.selectByPrimaryKey(loginInfo.getMemberId());
        if (member == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        if (StringUtils.isBlank(invoiceApplyVo.getOrderIdStr())) {
            return DataTResponse.FAIL("订单号列表为空");
        }

        logger.info("申请发票,{},{}", member.getMemberId(), invoiceApplyVo.getOrderIdStr());
        //检测参数
        DataTResponse DataTResponse = checkInvoiceParameter(invoiceApplyVo);
        if (null != DataTResponse) {
            return DataTResponse;
        }

        if("null".equals(invoiceApplyVo.getCompanyCredit())){
            invoiceApplyVo.setCompanyCredit(null);
        }

        TResponse tResponse = invoiceApplyService.createInvoice(invoiceApplyVo, member);
        DataTResponse dataTResponse = new DataTResponse();
        BeanUtils.copyProperties(tResponse, dataTResponse);
        return dataTResponse;
    }


    ///**
    // * 查找开票公司信息
    // */
    //@RequestMapping(value = "/queryInvoiceInfo", method = RequestMethod.POST)
    //@ResponseBody
    //@ApiOperation(value = "查找开票公司信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    //@ApiImplicitParams({
    //        @ApiImplicitParam(name = "companyName", paramType = "form", value = "公司名称", required = true)})
    //public DataTResponse<String> queryInvoiceInfo(String companyName) {
    //    String host = invoiceHost;
    //    String path = invoicePath;
    //    String method = invoiceMethod;
    //    String appcode = invoiceCode;
    //    Map<String, String> headers = new HashMap<String, String>();
    //    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
    //    headers.put("Authorization", "APPCODE " + appcode);
    //    //根据API的要求，定义相对应的Content-Type
    //    headers.put("Content-Type", "application/json; charset=UTF-8");
    //    Map<String, String> querys = new HashMap<String, String>();
    //    String bodys = "{\"name\":\"" + companyName + "\"}";
    //
    //    try {
    //        /**
    //         * 重要提示如下:
    //         * HttpUtils请从
    //         * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
    //         * 下载
    //         *
    //         * 相应的依赖请参照
    //         * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
    //         */
    //        HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
    //        System.out.println(response.toString());
    //        //获取response的body
    //        return DataTResponse.SUCCESS_DATA(EntityUtils.toString(response.getEntity()));
    //    } catch (Exception e) {
    //        logger.error(e.getMessage());
    //        return DataTResponse.FAIL();
    //
    //    }
    //}

    /**
     * 发票申请详情
     */
    @RequestMapping(value = "/selectInvoiceApplyById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "发票详情(需要登录)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invoiceApplyId", value = "申请id 二者选其一", paramType = "query", required = false),
            @ApiImplicitParam(name = "orderId", value = "订单id 二者选其一", paramType = "query", required = false),
    })
    public DataTResponse<InvoiceApplyVo> selectInvoviceApplyById(HttpServletRequest request, Long invoiceApplyId, Long orderId) {
        String tenantCode = (String) request.getSession().getAttribute(SessionConstants.SESSION_TENANT);
        if (tenantCode == null || StringUtils.isBlank(tenantCode)) {
            logger.error("找不到对应运营商信息:{}", tenantCode);
            return DataTResponse.FAIL("找不到对应运营商信息");
        }
        LoginInfoVo loginInfo = readLoginInfo(request);
        if (loginInfo == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        MemberInfo member = memberInfoService.selectByPrimaryKey(loginInfo.getMemberId());
        if (member == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        logger.info("InvoiceController.selectInvoiceApplyById.tenantCode:{},mobile:{}", tenantCode, member.getMobile());

        if (invoiceApplyId == null&&orderId==null) {
            return DataTResponse.FAIL();
        }
        InvoiceApplyVo invoiceApply = new InvoiceApplyVo();
        if (orderId != null && !"".equals(orderId)) {

            invoiceApply = invoiceApplyService.selectInvoiceApplyByOrderId(orderId);
        }
        if (invoiceApplyId != null && !"".equals(invoiceApplyId)) {

            invoiceApply = invoiceApplyService.selectInvoiceApplyById(invoiceApplyId);
        }

        return DataTResponse.SUCCESS_DATA(invoiceApply);
    }

    ///**
    // * 已开票订单年月
    // */
    //@ResponseBody
    //@RequestMapping(value = "/listOrderDateWithInvoice", method = RequestMethod.POST)
    //@ApiOperation(value = "已开票订单年月", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    //@ApiImplicitParams({
    //        @ApiImplicitParam(name = "invoiceApplyId", value = "申请id", paramType = "query", required = true),
    //})
    //public DataTResponse<List<String>> listOrderDateWithInvoice(HttpServletRequest request, @RequestParam(value = "invoiceApplyId") Long invoiceApplyId) {
    //    String tenantCode = (String) request.getSession().getAttribute(SessionConstants.SESSION_TENANT);
    //    LoginInfoVo loginInfo = readLoginInfo(request);
    //    if (loginInfo == null) {
    //        return DataTResponse.FAIL_LOGIN();
    //    }
    //    MemberInfo member = memberInfoService.selectByPrimaryKey(loginInfo.getMemberId());
    //    if (member == null) {
    //        return DataTResponse.FAIL_LOGIN();
    //    }
    //    if (invoiceApplyId == null) {
    //        return DataTResponse.FAIL("发票申请id不能为空");
    //    }
    //    List<String> list = orderInfoService.listOrderDateWithInvoice(member, invoiceApplyId);
    //    return DataTResponse.SUCCESS_DATA(list);
    //}
    //
    //
    ///**
    // * 已开票订单
    // */
    //@RequestMapping(value = "/listPageOrderWithInvovice", method = RequestMethod.POST)
    //@ResponseBody
    //@ApiOperation(value = "已开票订单(需要登录)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    //@ApiImplicitParams({
    //        @ApiImplicitParam(name = "pageNo", value = "", paramType = "query", required = false),
    //        @ApiImplicitParam(name = "pageSize", value = "", paramType = "query", required = false),
    //        @ApiImplicitParam(name = "yearMonth", paramType = "query", value = "年月（企业开票用到）", required = true),
    //        @ApiImplicitParam(name = "invoiceApplyId", value = "申请id", paramType = "query", required = true),
    //})
    //public DataTResponse<PageInfo<OrderInfoVo>> listPageOrderWithInvovice(HttpServletRequest request, @RequestParam(value = "invoiceApplyId") Long invoiceApplyId,
    //                                                                      @RequestParam(value = "yearMonth") String yearMonth,
    //                                                                      @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
    //                                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
    //    String tenantCode = (String) request.getSession().getAttribute(SessionConstants.SESSION_TENANT);
    //    LoginInfoVo loginInfo = readLoginInfo(request);
    //    if (loginInfo == null) {
    //        return DataTResponse.FAIL_LOGIN();
    //    }
    //    MemberInfo member = memberInfoService.selectByPrimaryKey(loginInfo.getMemberId());
    //    if (member == null) {
    //        return DataTResponse.FAIL_LOGIN();
    //    }
    //
    //    if (invoiceApplyId == null) {
    //        return DataTResponse.FAIL("发票申请id不能为空");
    //    }
    //    if (yearMonth == null) {
    //        return DataTResponse.FAIL("年月不能为空");
    //    }
    //    PageInfo<OrderInfoVo> pageInfo = orderInfoService.listPageOrderWithInvovice(member, yearMonth, invoiceApplyId, pageNo, pageSize);
    //    return DataTResponse.SUCCESS_DATA(pageInfo);
    //}


    /**
     * 校验开具发票参数
     */
    private DataTResponse checkInvoiceParameter(InvoiceApplyVo invoiceVo) {
        if (invoiceVo == null) {
            return DataTResponse.FAIL("参数为空");
        }
        if (invoiceVo.getInvoiceType() == null || invoiceVo.getInvoiceType() == "") {
            return DataTResponse.FAIL("请选择发票类型");
        }
        if (invoiceVo.getAmount() == null) {
            return DataTResponse.FAIL("开票金额为空");
        }


        if (StringUtils.isBlank(invoiceVo.getCompanyEmail())) {
            return DataTResponse.FAIL("请输入邮箱地址");
        }
        //if (StringUtils.isBlank(invoiceVo.getAddress()) && StringUtils.isBlank(invoiceVo.getCompanyEmail())) {
        //    return DataTResponse.FAIL("请输入收件人地址或邮箱地址");
        //}
        //if (StringUtils.isBlank(invoiceVo.getAddresseeTel()) && StringUtils.isBlank(invoiceVo.getCompanyEmail())) {
        //    return DataTResponse.FAIL("请输入收件人联系电话或邮箱地址");
        //}
        //if (StringUtils.isBlank(invoiceVo.getAddressee()) && StringUtils.isBlank(invoiceVo.getCompanyEmail())) {
        //    return DataTResponse.FAIL("请输入收件人或邮箱地址");
        //}

        if ("1".equals(invoiceVo.getInvoiceType())) {
            if (invoiceVo.getCompanyCredit() == null) {
                return DataTResponse.FAIL("请输入纳税人识别号");
            }
            if (StringUtils.isBlank(invoiceVo.getCompanyBank())) {
                return DataTResponse.FAIL("请输入开户行");
            }
            if (StringUtils.isBlank(invoiceVo.getCompanyBankAccount())) {
                return DataTResponse.FAIL("请输入开户行账号");
            }
            if (StringUtils.isBlank(invoiceVo.getCompanyAddress())) {
                return DataTResponse.FAIL("请输入抬头公司地址");
            }
            if (StringUtils.isBlank(invoiceVo.getCompanyTel())) {
                return DataTResponse.FAIL("请输入抬头公司联系电话");
            }
        }
        return null;
    }

    /**
     * 检验发票邮寄地址参数
     */
    private DataTResponse checkInvoiceAddressParameter(InvoiceAddress invoiceAddress) {
        if (invoiceAddress == null) {
            return DataTResponse.FAIL("邮寄地址参数为空");
        }
        if (StringUtils.isBlank(invoiceAddress.getAddress())) {
            return DataTResponse.FAIL("请输入邮寄地址");
        }
        if (StringUtils.isBlank(invoiceAddress.getAddressee())) {
            return DataTResponse.FAIL("请输入收件人姓名");
        }

        if (StringUtils.isBlank(invoiceAddress.getAddresseeTel())) {
            return DataTResponse.FAIL("请输入收件人手机号");
        }
        if (StringUtils.isBlank(invoiceAddress.getPostcode())) {
            return DataTResponse.FAIL("请输入邮编");
        }
        return null;
    }

    /**
     * 检验会员发票信息参数
     */
    private DataTResponse checkMemberInvoiceInfoParameter(MemberInvoiceInfo memberInvoiceInfo) {
        if (memberInvoiceInfo == null) {
            return DataTResponse.FAIL("参数不能为空");
        }
        if ("1".equals(memberInvoiceInfo.getInvoiceType())) {
            if (StringUtils.isEmpty(memberInvoiceInfo.getInvoiceType())) {
                return DataTResponse.FAIL("发票类型不能为空");
            }
            if (StringUtils.isEmpty(memberInvoiceInfo.getInvoiceNormalType())) {
                return DataTResponse.FAIL("开票类型不能为空");
            }
//            if (!BeanUtil.isValid(memberInvoiceInfo.getCompanyName())){
//                return DataTResponse.FAIL("公司名不能为空");
//            }
            if (StringUtils.isEmpty(memberInvoiceInfo.getCompanyCredit())) {
                return DataTResponse.FAIL("纳税人识别号不能为空");
            }
            if (StringUtils.isEmpty(memberInvoiceInfo.getCompanyTel())) {
                return DataTResponse.FAIL("公司联系电话不能为空");
            }
            if (StringUtils.isEmpty(memberInvoiceInfo.getCompanyBank())) {
                return DataTResponse.FAIL("开户行不能为空");
            }
            if (StringUtils.isEmpty(memberInvoiceInfo.getCompanyBankAccount())) {
                return DataTResponse.FAIL("公司银行账号不能为空");
            }
//            if (!BeanUtil.isValid(memberInvoiceInfo.getProvinceId())){
//                return DataTResponse.FAIL("省份不能为空");
//            }
//            if (!BeanUtil.isValid(memberInvoiceInfo.getCityId())){
//                return DataTResponse.FAIL("城市不能为空");
//            }
//            if (!BeanUtil.isValid(memberInvoiceInfo.getAreaId())){
//                return DataTResponse.FAIL("县(区)不能为空");
//            }
            if (StringUtils.isEmpty(memberInvoiceInfo.getCompanyAddress())) {
                return DataTResponse.FAIL("公司地址不能为空");
            }
            if (StringUtils.isEmpty(memberInvoiceInfo.getRegisterAddress())) {
                return DataTResponse.FAIL("公司注册地址不能为空");
            }
            if (StringUtils.isEmpty(memberInvoiceInfo.getRegisterTel())) {
                return DataTResponse.FAIL("公司注册电话不能为空");
            }
        } else if ("0".equals(memberInvoiceInfo.getInvoiceType())) {
            if (StringUtils.isEmpty(memberInvoiceInfo.getCompanyEmail())) {
                return DataTResponse.FAIL("公司邮箱不能为空");
            }
        }
        if (StringUtils.isEmpty(memberInvoiceInfo.getInvoiceTitle())) {
            return DataTResponse.FAIL("开票抬头不能为空");
        }
        if (StringUtils.isEmpty(memberInvoiceInfo.getCompanyCredit())) {
            return DataTResponse.FAIL("纳税人识别号不能为空");
        }
        return DataTResponse.SUCCESS();
    }


    /**
     * 增、改
     */
    @ApiOperation(value = "增、改 会员发票抬头", httpMethod = "POST")
    @RequestMapping(value = "/saveOrUpdateInvoiceAddress", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<String> saveOrUpdateInvoiceAddress(HttpServletRequest request, InvoiceAddress invoiceAddress) {
        LoginInfoVo loginInfo = readLoginInfo(request);
        invoiceAddress.setTenantCode(loginInfo.getTenantCode());
        invoiceAddress.setMemberId(loginInfo.getMemberId());
        if(invoiceAddress.getInvoiceTitle()==null&&invoiceAddress.getInvoiceAddressId()==null){
            return DataTResponse.FAIL("参数错误");
        }
        invoiceAddressService.saveOrUpdateInvoiceAddress(invoiceAddress);
        return DataTResponse.SUCCESS();
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询单个 会员发票抬头", httpMethod = "POST")
    @RequestMapping(value = "/selectByInvoiceAddressId", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invoiceAddressId", paramType = "query", value = "会员发票抬头id", required = false)})
    private DataTResponse<InvoiceAddress> selectByInvoiceAddressId(HttpServletRequest request, Long invoiceAddressId) {
        LoginInfoVo loginInfoVo = readLoginInfo(request);
        if (loginInfoVo == null || loginInfoVo.getMemberId() == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        InvoiceAddress invoiceAddress = invoiceAddressService.selectByPrimaryKey(invoiceAddressId);
        if (!invoiceAddress.getMemberId().equals(loginInfoVo.getMemberId())) {
            invoiceAddress = null;
        }
        return DataTResponse.SUCCESS_DATA(invoiceAddress);
    }

    /**
     * 查询列表
     */
    @ApiOperation(value = "查询列表 会员发票抬头", httpMethod = "POST")
    @RequestMapping(value = "/listByInvoiceAddress", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<List<InvoiceAddress>> listByInvoiceAddress(HttpServletRequest request) {
        LoginInfoVo loginInfoVo = readLoginInfo(request);
        if (loginInfoVo == null || loginInfoVo.getMemberId() == null) {
            return DataTResponse.FAIL_LOGIN();
        }

        InvoiceAddress invoiceAddress = new InvoiceAddress();
        invoiceAddress.setMemberId(loginInfoVo.getMemberId());
        invoiceAddress.setStatus("0");

        List<InvoiceAddress> list = invoiceAddressService.listByCondition(invoiceAddress);
        return DataTResponse.SUCCESS_DATA(list);
    }


    /**
     * 删除发票抬头
     */
    @ApiOperation(value = "删除发票抬头", httpMethod = "POST")
    @RequestMapping(value = "/deleteByInvoiceAddressId", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invoiceAddressId", paramType = "query", value = "会员发票抬头id", required = false)})
    private DataTResponse<String> deleteByInvoiceAddressId(HttpServletRequest request,Long invoiceAddressId) {
        LoginInfoVo loginInfoVo = readLoginInfo(request);
        if (loginInfoVo == null || loginInfoVo.getMemberId() == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        InvoiceAddress invoiceAddress = invoiceAddressService.selectByPrimaryKey(invoiceAddressId);
        if (!invoiceAddress.getMemberId().equals(loginInfoVo.getMemberId())) {
            return DataTResponse.FAIL("只能删除用户自己的发票抬头");
        }
        invoiceAddress.setStatus("1");
        invoiceAddress.setLastUpdatedTime(new Date());
        invoiceAddressService.updateByPrimaryKey(invoiceAddress);
        return DataTResponse.SUCCESS();
    }

    /**
     * 查询列表分页
     */
    @ApiOperation(value = "查询列表分页 会员发票抬头", httpMethod = "POST")
    @RequestMapping(value = "/listPageByInvoiceAddress", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<List<InvoiceAddress>> listPageByInvoiceAddress(HttpServletRequest request, RcPageVo rcPageVo) {
        LoginInfoVo loginInfoVo = readLoginInfo(request);
        if (loginInfoVo == null || loginInfoVo.getMemberId() == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        //Page
        PageHelper.startPage(rcPageVo.getPageNo(), rcPageVo.getPageSize());

        InvoiceAddress invoiceAddress = new InvoiceAddress();
        invoiceAddress.setMemberId(loginInfoVo.getMemberId());
        invoiceAddress.setStatus("0");
        List<InvoiceAddress> list = invoiceAddressService.listByCondition(invoiceAddress);
        PageInfo<InvoiceAddress> pageInfo = new PageInfo<>(list);
        return DataTResponse.SUCCESS_DATA(pageInfo);
    }


    /**
     * 查询列表分页
     */
    @ApiOperation(value = "查询列表分页 发票", httpMethod = "POST")
    @RequestMapping(value = "/listPageByInvoice", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<List<Invoice>> listPageByInvoice(HttpServletRequest request, RcPageVo rcPageVo) {
        LoginInfoVo loginInfoVo = readLoginInfo(request);
        if (loginInfoVo == null || loginInfoVo.getMemberId() == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        //Page
        PageHelper.startPage(rcPageVo.getPageNo(), rcPageVo.getPageSize());

        Invoice invoice = new Invoice();
        invoice.setMemberId(loginInfoVo.getMemberId());
        List<Invoice> list = invoiceService.listByCondition(invoice);
        PageInfo<Invoice> pageInfo = new PageInfo<>(list);
        return DataTResponse.SUCCESS_DATA(pageInfo);
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询默认会员发票抬头", httpMethod = "POST")
    @RequestMapping(value = "/selectByDefault", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<InvoiceAddress> selectByDefault(HttpServletRequest request) {
        LoginInfoVo loginInfoVo = readLoginInfo(request);
        if (loginInfoVo == null || loginInfoVo.getMemberId() == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        InvoiceAddressExample invoiceAddressExample=new InvoiceAddressExample();
        invoiceAddressExample.createCriteria().andMemberIdEqualTo(loginInfoVo.getMemberId()).andIsDefaultEqualTo("1").andStatusEqualTo("0");

        InvoiceAddress invoiceAddress = invoiceAddressService.selectByExample(invoiceAddressExample).stream().findFirst().orElseGet(()->null);
        return DataTResponse.SUCCESS_DATA(invoiceAddress);
    }

}
